This patch adds support for HFS+ compression.

Written by Mike Bombich.  Taken from http://www.bombich.com/rsync.html

Modified by Wayne to fix some issues and tweak the implementation a bit.
This compiles on OS X and passes the testsuite, but otherwise UNTESTED!

To use this patch, run these commands for a successful build:

    patch -p1 <patches/fileflags.diff
    patch -p1 <patches/crtimes.diff
    patch -p1 <patches/hfs-compression.diff
    ./prepare-source
    ./configure
    make

TODO:
 - Should rsync try to treat the compressed data as file data and use the
   rsync algorithm on the data transfer?

based-on: patch/b3.0.x/crtimes
diff --git a/flist.c b/flist.c
--- a/flist.c
+++ b/flist.c
@@ -1496,6 +1496,7 @@ static struct file_struct *send_file_name(int f, struct file_list *flist,
 #ifdef SUPPORT_FILEFLAGS
 			sx.st.st_flags = preserve_fileflags ? F_FFLAGS(file) : 0;
 #endif
+			sx.st.st_mtime = file->modtime; /* get_xattr needs mtime for decmpfs xattrs */
 			sx.xattr = NULL;
 			if (get_xattr(fname, &sx) < 0) {
 				io_error |= IOERR_GENERAL;
diff --git a/generator.c b/generator.c
--- a/generator.c
+++ b/generator.c
@@ -39,6 +39,7 @@ extern int keep_dirlinks;
 extern int force_change;
 extern int preserve_acls;
 extern int preserve_xattrs;
+extern int preserve_hfs_compression;
 extern int preserve_links;
 extern int preserve_devices;
 extern int preserve_specials;
@@ -1887,6 +1888,14 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
 					fname, fnamecmpbuf);
 			}
 			sx.st.st_size = F_LENGTH(fuzzy_file);
+#ifdef SUPPORT_HFS_COMPRESSION
+			if (sx.st.st_flags & UF_COMPRESSED) {
+				if (preserve_hfs_compression)
+					sx.st.st_size = 0;
+				else
+					sx.st.st_flags &= ~UF_COMPRESSED;
+			}
+#endif
 			statret = 0;
 			fnamecmp = fnamecmpbuf;
 			fnamecmp_type = FNAMECMP_FUZZY;
@@ -2071,6 +2080,18 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
 	if (read_batch)
 		goto cleanup;
 
+#ifdef SUPPORT_HFS_COMPRESSION
+	if (F_FFLAGS(file) & UF_COMPRESSED) {
+		/* At this point the attrs have already been copied, we don't need to transfer a data fork
+		 * If my filesystem doesn't support HFS compression, the existing file's content
+		 * will not be automatically truncated, so we'll do that manually here */
+		if (preserve_hfs_compression && sx.st.st_size > 0) {
+			if (ftruncate(fd, 0) == 0)
+				sx.st.st_size = 0;
+		}
+	}
+#endif
+
 	if (statret != 0 || whole_file)
 		write_sum_head(f_out, NULL);
 	else if (sx.st.st_size <= 0) {
diff --git a/lib/sysxattrs.c b/lib/sysxattrs.c
--- a/lib/sysxattrs.c
+++ b/lib/sysxattrs.c
@@ -22,6 +22,17 @@
 #include "rsync.h"
 #include "sysxattrs.h"
 
+extern int preserve_hfs_compression;
+
+#ifdef HAVE_OSX_XATTRS
+#ifndef XATTR_SHOWCOMPRESSION
+#define XATTR_SHOWCOMPRESSION 0x0020
+#endif
+#define GETXATTR_FETCH_LIMIT (64*1024*1024)
+
+int xattr_options = XATTR_NOFOLLOW;
+#endif
+
 #ifdef SUPPORT_XATTRS
 
 #if defined HAVE_LINUX_XATTRS
@@ -55,7 +66,27 @@ ssize_t sys_llistxattr(const char *path, char *list, size_t size)
 
 ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
 {
-	return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
+	ssize_t len;
+
+	if (preserve_hfs_compression)
+		xattr_options |= XATTR_SHOWCOMPRESSION;
+
+	len = getxattr(path, name, value, size, 0, xattr_options);
+
+	/* If we're retrieving data, handle resource forks > 64MB specially */
+	if (value != NULL && strcmp(name, XATTR_RESOURCEFORK_NAME) == 0 && len == GETXATTR_FETCH_LIMIT) {
+		/* getxattr will only return 64MB of data at a time, need to call again with a new offset */
+		u_int32_t offset = GETXATTR_FETCH_LIMIT;
+		ssize_t data_retrieved = len;
+		while (data_retrieved < (ssize_t)size) {
+			len = getxattr(path, name, value + offset, size - data_retrieved, offset, xattr_options);
+			data_retrieved += len;
+			offset += (u_int32_t)len;
+		}
+		len = data_retrieved;
+	}
+
+	return len;
 }
 
 ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
@@ -70,12 +101,16 @@ int sys_lsetxattr(const char *path, const char *name, const void *value, size_t
 
 int sys_lremovexattr(const char *path, const char *name)
 {
-	return removexattr(path, name, XATTR_NOFOLLOW);
+	if (preserve_hfs_compression)
+		xattr_options |= XATTR_SHOWCOMPRESSION;
+	return removexattr(path, name, xattr_options);
 }
 
 ssize_t sys_llistxattr(const char *path, char *list, size_t size)
 {
-	return listxattr(path, list, size, XATTR_NOFOLLOW);
+	if (preserve_hfs_compression)
+		xattr_options |= XATTR_SHOWCOMPRESSION;
+	return listxattr(path, list, size, xattr_options);
 }
 
 #elif HAVE_FREEBSD_XATTRS
diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -29,6 +29,10 @@
 #ifdef SUPPORT_FORCE_CHANGE
 #include <sys/sysctl.h>
 #endif
+#ifdef SUPPORT_HFS_COMPRESSION
+#include <sys/attr.h> /* For getattrlist() */
+#include <sys/mount.h> /* For statfs() */
+#endif
 
 extern int verbose;
 extern int dry_run;
@@ -50,7 +54,9 @@ extern int copy_dirlinks;
 extern int copy_unsafe_links;
 extern int keep_dirlinks;
 extern int preserve_hard_links;
+extern int preserve_hfs_compression;
 extern int protocol_version;
+extern int force_change;
 extern int file_total;
 extern int recurse;
 extern int xfer_dirs;
@@ -490,6 +496,43 @@ static pid_t do_cmd(char *cmd, char *machine, char *user, char **remote_argv, in
 	return 0; /* not reached */
 }
 
+#ifdef SUPPORT_HFS_COMPRESSION
+static void hfs_receiver_check(const char *dest_path)
+{
+	struct statfs fsb;
+	struct attrlist attrs;
+	struct {
+		int32_t len;
+		vol_capabilities_set_t caps;
+	} attrData;
+
+	if (preserve_hfs_compression != 1)
+		return; /* Nothing to check if --hfs-compression option isn't enabled. */
+
+	if (statfs(dest_path, &fsb) < 0) {
+		rsyserr(FERROR, errno, "statfs %s failed", full_fname(dest_path));
+		exit_cleanup(RERR_FILESELECT);
+	}
+
+	bzero(&attrs, sizeof attrs);
+	attrs.bitmapcount = ATTR_BIT_MAP_COUNT;
+	attrs.volattr = ATTR_VOL_CAPABILITIES;
+
+	bzero(&attrData, sizeof attrData);
+	attrData.len = sizeof attrData;
+
+	if (getattrlist(fsb.f_mntonname, &attrs, &attrData, sizeof attrData, 0) < 0) {
+		rsyserr(FERROR, errno, "getattrlist %s failed", full_fname(dest_path));
+		exit_cleanup(RERR_FILESELECT);
+	}
+
+	if (!(attrData.caps[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_DECMPFS_COMPRESSION)) {
+		rprintf(FERROR, "The destination filesystem does not support HFS+ compression.\n");
+		exit_cleanup(RERR_UNSUPPORTED);
+	}
+}
+#endif
+
 /* The receiving side operates in one of two modes:
  *
  * 1. it receives any number of files into a destination directory,
@@ -548,6 +591,9 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
 				exit_cleanup(RERR_FILESELECT);
 			}
 			filesystem_dev = st.st_dev; /* ensures --force works right w/-x */
+#ifdef SUPPORT_HFS_COMPRESSION
+			hfs_receiver_check(dest_path);
+#endif
 			return NULL;
 		}
 		if (file_total > 1) {
@@ -608,7 +654,9 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
 				full_fname(dest_path));
 			exit_cleanup(RERR_FILESELECT);
 		}
-
+#ifdef SUPPORT_HFS_COMPRESSION
+		hfs_receiver_check(dest_path);
+#endif
 		return NULL;
 	}
 
@@ -628,6 +676,9 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
 			full_fname(dest_path));
 		exit_cleanup(RERR_FILESELECT);
 	}
+#ifdef SUPPORT_HFS_COMPRESSION
+	hfs_receiver_check(dest_path);
+#endif
 	*cp = '/';
 
 	return cp + 1;
@@ -981,7 +1032,6 @@ int child_main(int argc, char *argv[])
 	return 0;
 }
 
-
 void start_server(int f_in, int f_out, int argc, char *argv[])
 {
 	set_nonblocking(f_in);
diff --git a/options.c b/options.c
--- a/options.c
+++ b/options.c
@@ -52,6 +52,7 @@ int preserve_links = 0;
 int preserve_hard_links = 0;
 int preserve_acls = 0;
 int preserve_xattrs = 0;
+int preserve_hfs_compression = 0;
 int preserve_perms = 0;
 int preserve_fileflags = 0;
 int preserve_executability = 0;
@@ -355,6 +356,10 @@ void usage(enum logcode F)
 #ifdef SUPPORT_XATTRS
   rprintf(F," -X, --xattrs                preserve extended attributes\n");
 #endif
+#ifdef SUPPORT_HFS_COMPRESSION
+  rprintf(F,"     --hfs-compression       preserve HFS compression if supported\n");
+  rprintf(F,"     --protect-decmpfs       preserve HFS compression as xattrs\n");
+#endif
   rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
   rprintf(F," -g, --group                 preserve group\n");
   rprintf(F,"     --devices               preserve device files (super-user only)\n");
@@ -588,6 +593,12 @@ static struct poptOption long_options[] = {
   {"force-uchange",    0,  POPT_ARG_VAL,    &force_change, USR_IMMUTABLE, 0, 0 },
   {"force-schange",    0,  POPT_ARG_VAL,    &force_change, SYS_IMMUTABLE, 0, 0 },
 #endif
+#ifdef SUPPORT_HFS_COMPRESSION
+  {"hfs-compression",  0,  POPT_ARG_VAL,    &preserve_hfs_compression, 1, 0, 0 },
+  {"no-hfs-compression",0, POPT_ARG_VAL,    &preserve_hfs_compression, 0, 0, 0 },
+  {"protect-decmpfs",  0,  POPT_ARG_VAL,    &preserve_hfs_compression, 2, 0, 0 },
+  {"no-protect-decmpfs",0, POPT_ARG_VAL,    &preserve_hfs_compression, 0, 0, 0 },
+#endif
   {"ignore-errors",    0,  POPT_ARG_VAL,    &ignore_errors, 1, 0, 0 },
   {"no-ignore-errors", 0,  POPT_ARG_VAL,    &ignore_errors, 0, 0, 0 },
   {"max-delete",       0,  POPT_ARG_INT,    &max_delete, 0, 0, 0 },
@@ -1362,6 +1373,15 @@ int parse_arguments(int *argc_p, const char ***argv_p)
 	}
 #endif
 
+#ifdef SUPPORT_HFS_COMPRESSION
+	if (preserve_hfs_compression) {
+		if (!preserve_xattrs)
+			preserve_xattrs = 1;
+		if (!preserve_fileflags)
+			preserve_fileflags = 1;
+	}
+#endif
+
 	if (write_batch && read_batch) {
 		snprintf(err_buf, sizeof err_buf,
 			"--write-batch and --read-batch can not be used together\n");
@@ -1915,6 +1935,11 @@ void server_options(char **args, int *argc_p)
 	if (preserve_fileflags)
 		args[ac++] = "--fileflags";
 
+#ifdef SUPPORT_HFS_COMPRESSION
+	if (preserve_hfs_compression)
+		args[ac++] = preserve_hfs_compression == 1 ? "--hfs-compression" : "--protect-decmpfs";
+#endif
+
 	if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
 		if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
 			goto oom;
diff --git a/rsync.c b/rsync.c
--- a/rsync.c
+++ b/rsync.c
@@ -498,8 +498,14 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
 #ifdef SUPPORT_XATTRS
 	if (am_root < 0)
 		set_stat_xattr(fname, file, new_mode);
-	if (preserve_xattrs && fnamecmp)
+	if (preserve_xattrs && fnamecmp) {
+		uint32 tmpflags = sxp->st.st_flags;
+		sxp->st.st_flags = F_FFLAGS(file); /* set_xattr() needs to check UF_COMPRESSED */
 		set_xattr(fname, file, fnamecmp, sxp);
+		sxp->st.st_flags = tmpflags;
+		if (S_ISDIR(sxp->st.st_mode))
+			link_stat(fname, &sx2.st, 0);
+	}
 #endif
 
 	if (!preserve_times
@@ -510,6 +516,9 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
 	if (sxp->st.st_ino == 2 && S_ISDIR(sxp->st.st_mode))
 		flags |= ATTRS_SKIP_CRTIME;
 	if (!(flags & ATTRS_SKIP_MTIME)
+#ifdef SUPPORT_HFS_COMPRESSION
+	    && !(sxp->st.st_flags & UF_COMPRESSED) /* setting this alters mtime, so defer to after set_fileflags */
+#endif
 	    && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
 		int ret = set_modtime(fname, file->modtime, sxp->st.st_mode, ST_FLAGS(sxp->st));
 		if (ret < 0) {
@@ -620,6 +629,16 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
 		 && !set_fileflags(fname, fileflags))
 			goto cleanup;
 		updated = 1;
+#ifdef SUPPORT_HFS_COMPRESSION
+		int ret = set_modtime(fname, file->modtime, new_mode, fileflags);
+		if (ret < 0) {
+			rsyserr(FERROR_XFER, errno, "failed to set times on %s",
+				full_fname(fname));
+			goto cleanup;
+		}
+		if (ret != 0)
+			file->flags |= FLAG_TIME_FAILED;
+#endif
 	}
 #endif
 
diff --git a/rsync.h b/rsync.h
--- a/rsync.h
+++ b/rsync.h
@@ -509,6 +509,17 @@ typedef unsigned int size_t;
 #define ST_FLAGS(st) NO_FFLAGS
 #endif
 
+#ifndef UF_COMPRESSED
+#define UF_COMPRESSED 0x00000020
+#endif
+#ifndef VOL_CAP_FMT_DECMPFS_COMPRESSION
+#define VOL_CAP_FMT_DECMPFS_COMPRESSION 0x00010000
+#endif
+
+#if defined SUPPORT_XATTRS && defined SUPPORT_FILEFLAGS
+#define SUPPORT_HFS_COMPRESSION 1
+#endif
+
 /* Find a variable that is either exactly 32-bits or longer.
  * If some code depends on 32-bit truncation, it will need to
  * take special action in a "#if SIZEOF_INT32 > 4" section. */
diff --git a/rsync.yo b/rsync.yo
--- a/rsync.yo
+++ b/rsync.yo
@@ -347,6 +347,8 @@ to the detailed description below for a complete description.  verb(
      --chmod=CHMOD           affect file and/or directory permissions
  -A, --acls                  preserve ACLs (implies -p)
  -X, --xattrs                preserve extended attributes
+     --hfs-compression       preserve HFS compression if supported
+     --protect-decmpfs       preserve HFS compression as xattrs
  -o, --owner                 preserve owner (super-user only)
  -g, --group                 preserve group
      --devices               preserve device files (super-user only)
@@ -1019,6 +1021,42 @@ flags on files and directories that are being updated or deleted on the
 receiving side.  It does not try to affect user flags.  This option overrides
 bf(--force-change) and bf(--force-schange).
 
+dit(bf(--hfs-compression)) This option causes rsync to preserve HFS+
+compression if the destination filesystem supports it.  If the destination
+does not support it, rsync will exit with an error.
+
+Filesystem compression was introduced to HFS+ in Mac OS 10.6. A file that is
+compressed has no data in its data fork. Rather, the compressed data is stored
+in an extended attribute named com.apple.decmpfs and a file flag is set to
+indicate that the file is compressed (UF_COMPRESSED). HFS+ decompresses this
+data "on-the-fly" and presents it to the operating system as a normal file.
+Normal attempts to copy compressed files (e.g. in the Finder, via cp, ditto,
+etc.) will copy the file's decompressed contents, remove the UF_COMPRESSED file
+flag, and discard the com.apple.decmpfs extended attribute. This option will
+preserve the data in the com.apple.decmpfs extended attribute and ignore the
+synthesized data in the file contents.
+
+This option implies both bf(--fileflags) and (--xattrs).
+
+dit(bf(--protect-decmpfs)) The com.apple.decmpfs extended attribute is hidden
+by default from list/get xattr calls, therefore normal attempts to copy
+compressed files will functionally decompress those files. While this is
+desirable behavior when copying files to filesystems that do not support HFS+
+compression, it has serious performance and capacity impacts when backing up
+or restoring the Mac OS X filesystem.
+
+This option will transfer the com.apple.decmpfs extended attribute regardless
+of support on the destination. If a source file is compressed and an existing
+file on the destination is not compressed, the data fork of the destination
+file will be truncated and the com.apple.decmpfs xattr will be transferred
+instead. Note that compressed files will not be readable to the operating
+system of the destination if that operating system does not support HFS+
+compression. Once restored (with or without this option) to an operating system
+that supports HFS+ compression, however, these files will be accessible as
+usual.
+
+This option implies bf(--fileflags) and bf(--xattrs).
+
 dit(bf(--chmod)) This option tells rsync to apply one or more
 comma-separated "chmod" strings to the permission of the files in the
 transfer.  The resulting value is treated as though it were the permissions
diff --git a/t_stub.c b/t_stub.c
--- a/t_stub.c
+++ b/t_stub.c
@@ -29,6 +29,7 @@ int module_dirlen = 0;
 int force_change = 0;
 int preserve_times = 0;
 int preserve_xattrs = 0;
+int preserve_hfs_compression = 0;
 mode_t orig_umask = 002;
 char *partial_dir;
 char *module_dir;
diff --git a/xattrs.c b/xattrs.c
--- a/xattrs.c
+++ b/xattrs.c
@@ -32,6 +32,7 @@ extern int am_generator;
 extern int read_only;
 extern int list_only;
 extern int preserve_xattrs;
+extern int preserve_hfs_compression;
 extern int preserve_links;
 extern int preserve_devices;
 extern int preserve_specials;
@@ -40,6 +41,10 @@ extern int checksum_seed;
 #define RSYNC_XAL_INITIAL 5
 #define RSYNC_XAL_LIST_INITIAL 100
 
+#define GXD_NO_MISSING_ERROR (1<<0)
+#define GXD_OMIT_COMPRESSED (1<<1)
+#define GXD_FILE_IS_COMPRESSED (1<<2)
+
 #define MAX_FULL_DATUM 32
 
 #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
@@ -72,6 +77,17 @@ extern int checksum_seed;
 #define XDEF_ACL_SUFFIX "dacl"
 #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
 
+#define APPLE_PREFIX "com.apple."
+#define APLPRE_LEN ((int)sizeof APPLE_PREFIX - 1)
+#define DECMPFS_SUFFIX "decmpfs"
+#define RESOURCEFORK_SUFFIX "ResourceFork"
+
+#define UNREAD_DATA ((char *)1)
+
+#if MAX_DIGEST_LEN < SIZEOF_TIME_T
+#error MAX_DIGEST_LEN is too small to hold an mtime
+#endif
+
 typedef struct {
 	char *datum, *name;
 	size_t datum_len, name_len;
@@ -163,8 +179,7 @@ static ssize_t get_xattr_names(const char *fname)
 /* On entry, the *len_ptr parameter contains the size of the extra space we
  * should allocate when we create a buffer for the data.  On exit, it contains
  * the length of the datum. */
-static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
-			    int no_missing_error)
+static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr, int flags)
 {
 	size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
 	size_t extra_len = *len_ptr;
@@ -173,7 +188,7 @@ static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr
 	*len_ptr = datum_len;
 
 	if (datum_len == (size_t)-1) {
-		if (errno == ENOTSUP || no_missing_error)
+		if (errno == ENOTSUP || flags & GXD_NO_MISSING_ERROR)
 			return NULL;
 		rsyserr(FERROR_XFER, errno,
 			"get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
@@ -181,6 +196,15 @@ static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr
 		return NULL;
 	}
 
+	if (flags & GXD_OMIT_COMPRESSED && datum_len > MAX_FULL_DATUM
+	 && HAS_PREFIX(name, APPLE_PREFIX)
+	 && (strcmp(name+APLPRE_LEN, DECMPFS_SUFFIX) == 0
+	  || (flags & GXD_FILE_IS_COMPRESSED && strcmp(name+APLPRE_LEN, RESOURCEFORK_SUFFIX) == 0))) {
+		/* If we are omitting compress-file-related data, we don't want to
+		 * actually read this data. */
+		return UNREAD_DATA;
+	}
+
 	if (!datum_len && !extra_len)
 		extra_len = 1; /* request non-zero amount of memory */
 	if (datum_len + extra_len < datum_len)
@@ -209,7 +233,29 @@ static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr
 	return ptr;
 }
 
-static int rsync_xal_get(const char *fname, item_list *xalp)
+static void checksum_xattr_data(char *sum, const char *datum, size_t datum_len, stat_x *sxp)
+{
+	if (datum == UNREAD_DATA) {
+		/* For abbreviated compressed data, we store the file's mtime as the checksum. */
+		SIVAL(sum, 0, sxp->st.st_mtime);
+#if SIZEOF_TIME_T > 4
+		SIVAL(sum, 4, sxp->st.st_mtime >> 32);
+#if MAX_DIGEST_LEN > 8
+		memset(sum + 8, 0, MAX_DIGEST_LEN - 8);
+#endif
+#else
+#if MAX_DIGEST_LEN > 4
+		memset(sum + 4, 0, MAX_DIGEST_LEN - 4);
+#endif
+#endif
+	} else {
+		sum_init(checksum_seed);
+		sum_update(datum, datum_len);
+		sum_end(sum);
+	}
+}
+
+static int rsync_xal_get(const char *fname, stat_x *sxp)
 {
 	ssize_t list_len, name_len;
 	size_t datum_len, name_offset;
@@ -218,7 +264,8 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
 	int user_only = am_sender ? 0 : am_root <= 0;
 #endif
 	rsync_xa *rxa;
-	int count;
+	int count, flags;
+	item_list *xalp = sxp->xattr;
 
 	/* This puts the name list into the "namebuf" buffer. */
 	if ((list_len = get_xattr_names(fname)) < 0)
@@ -248,20 +295,22 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
 		}
 
 		datum_len = name_len; /* Pass extra size to get_xattr_data() */
-		if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
+		flags = GXD_OMIT_COMPRESSED;
+		if (preserve_hfs_compression && sxp->st.st_flags & UF_COMPRESSED)
+			flags |= GXD_FILE_IS_COMPRESSED;
+		if (!(ptr = get_xattr_data(fname, name, &datum_len, flags)))
 			return -1;
 
 		if (datum_len > MAX_FULL_DATUM) {
 			/* For large datums, we store a flag and a checksum. */
+			char *datum = ptr;
 			name_offset = 1 + MAX_DIGEST_LEN;
-			sum_init(checksum_seed);
-			sum_update(ptr, datum_len);
-			free(ptr);
-
 			if (!(ptr = new_array(char, name_offset + name_len)))
 				out_of_memory("rsync_xal_get");
 			*ptr = XSTATE_ABBREV;
-			sum_end(ptr + 1);
+			checksum_xattr_data(ptr+1, datum, datum_len, sxp);
+			if (datum != UNREAD_DATA)
+				free(datum);
 		} else
 			name_offset = datum_len;
 
@@ -306,7 +355,7 @@ int get_xattr(const char *fname, stat_x *sxp)
 			return 0;
 	}
 
-	if (rsync_xal_get(fname, sxp->xattr) < 0) {
+	if (rsync_xal_get(fname, sxp) < 0) {
 		free_xattr(sxp);
 		return -1;
 	}
@@ -357,6 +406,10 @@ int copy_xattrs(const char *source, const char *dest)
 
 static int find_matching_xattr(item_list *xalp)
 {
+#ifdef HAVE_OSX_XATTRS
+	xalp = NULL;
+	return -1; /* find_matching_xattr is a waste of cycles for MOSX clients */
+#else
 	size_t i, j;
 	item_list *lst = rsync_xal_l.items;
 
@@ -390,6 +443,7 @@ static int find_matching_xattr(item_list *xalp)
 	}
 
 	return -1;
+#endif
 }
 
 /* Store *xalp on the end of rsync_xal_l */
@@ -569,7 +623,8 @@ void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
 
 			/* Re-read the long datum. */
 			if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0))) {
-				rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
+				if (errno != ENOTSUP && errno != ENOATTR)
+					rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
 				write_varint(f_out, 0);
 				continue;
 			}
@@ -791,7 +846,7 @@ static int rsync_xal_set(const char *fname, item_list *xalp,
 	int user_only = am_root <= 0;
 #endif
 	size_t name_len;
-	int ret = 0;
+	int flags, ret = 0;
 
 	/* This puts the current name list into the "namebuf" buffer. */
 	if ((list_len = get_xattr_names(fname)) < 0)
@@ -803,7 +858,10 @@ static int rsync_xal_set(const char *fname, item_list *xalp,
 		if (XATTR_ABBREV(rxas[i])) {
 			/* See if the fnamecmp version is identical. */
 			len = name_len = rxas[i].name_len;
-			if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
+			flags = GXD_OMIT_COMPRESSED | GXD_NO_MISSING_ERROR;
+			if (preserve_hfs_compression && sxp->st.st_flags & UF_COMPRESSED)
+				flags |= GXD_FILE_IS_COMPRESSED;
+			if ((ptr = get_xattr_data(fnamecmp, name, &len, flags)) == NULL) {
 			  still_abbrev:
 				if (am_generator)
 					continue;
@@ -817,9 +875,7 @@ static int rsync_xal_set(const char *fname, item_list *xalp,
 				goto still_abbrev;
 			}
 
-			sum_init(checksum_seed);
-			sum_update(ptr, len);
-			sum_end(sum);
+			checksum_xattr_data(sum, ptr, len, sxp);
 			if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
 				free(ptr);
 				goto still_abbrev;
@@ -888,6 +944,10 @@ static int rsync_xal_set(const char *fname, item_list *xalp,
 		}
 	}
 
+#ifdef HAVE_OSX_XATTRS
+	rsync_xal_free(xalp); /* Free this because we aren't using find_matching_xattr(). */
+#endif
+
 	return ret;
 }
 
@@ -934,7 +994,7 @@ char *get_xattr_acl(const char *fname, int is_access_acl, size_t *len_p)
 {
 	const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
 	*len_p = 0; /* no extra data alloc needed from get_xattr_data() */
-	return get_xattr_data(fname, name, len_p, 1);
+	return get_xattr_data(fname, name, len_p, GXD_NO_MISSING_ERROR);
 }
 
 int set_xattr_acl(const char *fname, int is_access_acl, const char *buf, size_t buf_len)
@@ -1077,11 +1137,33 @@ int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
 	return 0;
 }
 
+#ifdef SUPPORT_HFS_COMPRESSION
+static inline void hfs_compress_tweaks(STRUCT_STAT *fst)
+{
+	if (fst->st_flags & UF_COMPRESSED) {
+		if (preserve_hfs_compression) {
+			/* We're sending the compression xattr, not the decompressed data fork.
+			 * Setting rsync's idea of the file size to 0 effectively prevents the
+			 * transfer of the data fork. */
+			fst->st_size = 0;
+		} else {
+			/* If the sender's filesystem supports compression, then we'll be able
+			 * to send the decompressed data fork and the decmpfs xattr will be
+			 * hidden (not sent). As such, we need to strip the compression flag. */
+			fst->st_flags &= ~UF_COMPRESSED;
+		}
+	}
+}
+#endif
+
 int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
 {
 	int ret = do_stat(fname, fst);
 	if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
 		xst->st_mode = 0;
+#ifdef SUPPORT_HFS_COMPRESSION
+	hfs_compress_tweaks(fst);
+#endif
 	return ret;
 }
 
@@ -1090,6 +1172,9 @@ int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
 	int ret = do_lstat(fname, fst);
 	if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
 		xst->st_mode = 0;
+#ifdef SUPPORT_HFS_COMPRESSION
+	hfs_compress_tweaks(fst);
+#endif
 	return ret;
 }
 
@@ -1098,6 +1183,9 @@ int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
 	int ret = do_fstat(fd, fst);
 	if ((ret < 0 || get_stat_xattr(NULL, fd, fst, xst) < 0) && xst)
 		xst->st_mode = 0;
+#ifdef SUPPORT_HFS_COMPRESSION
+	hfs_compress_tweaks(fst);
+#endif
 	return ret;
 }
 
diff -up a/rsync.1 b/rsync.1
--- a/rsync.1
+++ b/rsync.1
@@ -422,6 +422,8 @@ to the detailed description below for a
      \-\-chmod=CHMOD           affect file and/or directory permissions
  \-A, \-\-acls                  preserve ACLs (implies \-p)
  \-X, \-\-xattrs                preserve extended attributes
+     \-\-hfs\-compression       preserve HFS compression if supported
+     \-\-protect\-decmpfs       preserve HFS compression as xattrs
  \-o, \-\-owner                 preserve owner (super\-user only)
  \-g, \-\-group                 preserve group
      \-\-devices               preserve device files (super\-user only)
@@ -1175,6 +1177,44 @@ flags on files and directories that are
 receiving side.  It does not try to affect user flags.  This option overrides
 \fB\-\-force\-change\fP and \fB\-\-force\-schange\fP.
 .IP 
+.IP "\fB\-\-hfs\-compression\fP"
+This option causes rsync to preserve HFS+
+compression if the destination filesystem supports it.  If the destination
+does not support it, rsync will exit with an error.
+.IP 
+Filesystem compression was introduced to HFS+ in Mac OS 10.6. A file that is
+compressed has no data in its data fork. Rather, the compressed data is stored
+in an extended attribute named com.apple.decmpfs and a file flag is set to
+indicate that the file is compressed (UF_COMPRESSED). HFS+ decompresses this
+data \(dq\&on\-the\-fly\(dq\& and presents it to the operating system as a normal file.
+Normal attempts to copy compressed files (e.g. in the Finder, via cp, ditto,
+etc.) will copy the file\(cq\&s decompressed contents, remove the UF_COMPRESSED file
+flag, and discard the com.apple.decmpfs extended attribute. This option will
+preserve the data in the com.apple.decmpfs extended attribute and ignore the
+synthesized data in the file contents.
+.IP 
+This option implies both \fB\-\-fileflags\fP and (\-\-xattrs).
+.IP 
+.IP "\fB\-\-protect\-decmpfs\fP"
+The com.apple.decmpfs extended attribute is hidden
+by default from list/get xattr calls, therefore normal attempts to copy
+compressed files will functionally decompress those files. While this is
+desirable behavior when copying files to filesystems that do not support HFS+
+compression, it has serious performance and capacity impacts when backing up
+or restoring the Mac OS X filesystem.
+.IP 
+This option will transfer the com.apple.decmpfs extended attribute regardless
+of support on the destination. If a source file is compressed and an existing
+file on the destination is not compressed, the data fork of the destination
+file will be truncated and the com.apple.decmpfs xattr will be transferred
+instead. Note that compressed files will not be readable to the operating
+system of the destination if that operating system does not support HFS+
+compression. Once restored (with or without this option) to an operating system
+that supports HFS+ compression, however, these files will be accessible as
+usual.
+.IP 
+This option implies \fB\-\-fileflags\fP and \fB\-\-xattrs\fP.
+.IP 
 .IP "\fB\-\-chmod\fP"
 This option tells rsync to apply one or more
 comma\-separated \(dq\&chmod\(dq\& strings to the permission of the files in the
